﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LinqToSql
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (AdventureLiteDataContext aw = new AdventureLiteDataContext())
            {

                var results = from c in aw.Customers
                              from o in c.Orders
                              from oi in o.OrderItems
                              orderby c.FirstName
                              where c.FirstName.Length >= 5 &&
                                    oi.ProductInformation.Name == "HL Road Frame - Red, 44"
                              group oi by c into orders
                              select new
                              {
                                  Name = orders.Key.FirstName + " " + orders.Key.LastName,
                                  AverageOrder = orders.Average(oi => oi.Quantity)
                              };
                foreach (var item in results)
                {
                    MessageBox.Show(item.Name + " " + item.AverageOrder);
                }
                
            }
        }
    }
}
